package com.service.impl;

import cn.hutool.extra.pinyin.PinyinUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mapper.UserMapper;
import com.pojo.User;
import com.service.UserService;
import com.vo.UserVo;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceimpl implements UserService {

    @Resource
    private UserMapper userMapper;
    @Override
    public User selectByphone(String code) {
        User user = userMapper.selectByUserCode(code);
        return user;
    }

    @Override
    public PageInfo<User> selectUserall(UserVo userVo) {
        PageHelper.startPage(userVo.getPage(),userVo.getLimit());
        List<User> users = userMapper.selectUserall(userVo);
        PageInfo<User> pageInfo=new PageInfo<>(users);
        return pageInfo;
    }

    @Override
    public int insertSelective(User user) {
        //加密
        //生成一个UUID随机字符串
      /*  String str = UUID.randomUUID().toString();*/
        String pwd = new Md5Hash(user.getPassword(),"qwert").toString();
        user.setPassword(pwd);

        //设置userCode ，全拼
        String pinyin = PinyinUtil.getPinyin(user.getUsername(), "");
        user.setUsercode(pinyin);

        user.setSalt("qwert");
        int i = userMapper.insertSelective(user);
        return i;
    }

    @Override
    public int updateByPrimaryKeySelective(User record) {
        int i = userMapper.updateByPrimaryKeySelective(record);
        return i;
    }
    @Override
    public List<User> selectUserByPlotId(Integer plotid) {
        List<User> userList = userMapper.selectUserByPlotId(plotid);
        return userList;
    }

    @Override
    public List<User> selectAllUserByProtId() {
        List<User> userList = userMapper.selectAllUserByPort();
        return userList;
    }

    @Override
    public User selectUserById(Integer id) {
        User user = userMapper.selectUserById(id);
        return user;
    }

    @Override
    public User selectUserByName(String name) {
        User user = userMapper.selectUserByName(name);
        return user;
    }
}
